import moment from 'dayjs'
import request from './request'
import format from './formatAmt'

//导出excel
const exportExcel = function (result, excelName) {
  const blob = new Blob([result], { type: 'application/vnd.ms-excel;charset=utf-8' })
  const fileName = excelName + ".xlsx"
  const alink = document.createElement('a')
  alink.download = fileName //下载的名字
  alink.href = URL.createObjectURL(blob) //转换成url
  document.body.appendChild(alink)
  alink.click()
  URL.revokeObjectURL(alink.href) // 释放URL 对象
  document.body.removeChild(alink)
}
//过滤时间
let formatTime = function (row, column, cellValue, index, time = 'YYYY-MM-DD HH:mm:ss') {
  return cellValue ? moment(cellValue).format(time) : ''
}
//格式化日期时间
let formatDate = function (date, time = 'YYYY-MM-DD HH:mm:ss') {
  return date ? moment(date).format(time) : ''
}
let formatD = function (date, time = 'YYYY-MM-DD') {
  return date ? moment(date).format(time) : ''
}
//金额添加千分符
let formatMoney = function (row, column, cellValue, index) {
  return format.formatMoney(cellValue ? cellValue : 0, 2)
}

//数字添加千分符
let formatNum = function (row, column, cellValue, index) {
  return format.formatMoney(cellValue ? cellValue : 0, 0)
}

//金额千分符
let formatAmount = function (amount, num) {
  return format.formatMoney(amount ? amount : '', num)
}
//获取本日，本周，本月
let getTime = function (type) {
  let startDay = ''
  let endDay = ''
  switch (type) {
    case 'day':
      startDay = moment().format('YYYY-MM-DD')
      endDay = moment().format('YYYY-MM-DD')
      return { startDay, endDay }
    case 'week':
      startDay = moment().startOf('week').format('YYYY-MM-DD');
      endDay = moment().endOf('week').format('YYYY-MM-DD');
      return { startDay, endDay }
    case 'month':
      startDay = moment().startOf('month').format('YYYY-MM-DD');
      endDay = moment().endOf('month').format('YYYY-MM-DD');
      return { startDay, endDay }
  }
}
//获取枚举
let enumsList = async function (type) {
  let url = `/dict/queryList/${type}`
  let result = await request.httpRequest('GET', url)
  if (result.code == 200) {
    return result.data
  }
}
let cashList = async function (code) {
  let url = `/params/getByCode/${code}`
  let result = await request.mobileRequest('GET', url)
  if (result.code == 200) {
    return result.data
  }
}
let articleList = async function (type) {
  let url = `/article/getByType/${type}`
  let result = await request.mobileRequest('GET', url)
  if (result.code == 200) {
    return result.data
  }
}
const matchFileSuffixType = (val, type) => {
  const suffix = val.substring(val.lastIndexOf('.') + 1, val.length)
  let result = null
  if (type === 'video') result = ['mp4', 'm2v', 'mkv'].some((item) => item === suffix)
  else result = ['png', 'jpg', 'jpeg', 'bmp', 'gif'].some((item) => item === suffix)
  return result
}
export default {
  formatD,
  formatTime,//过滤时间
  formatDate,//格式化日期时间
  formatMoney,//金额格式化
  formatNum,//数字添加千分符
  formatAmount,//金额千分符
  getTime,//获取本日，本周，本月
  enumsList,//枚举
  cashList,//参数
  articleList,
  exportExcel,//导出excel
  matchFileSuffixType
}